package com.wuye.service;

import com.wuye.model.customer.*;
import com.wuye.repository.CustomerRepository;
import com.wuye.repository.OptionDetailRepository;
import com.wuye.web.Constant;
import org.apache.commons.collections.ArrayStack;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by zhanchi on 2016/6/30.
 */
@Service
public class CustomerSuggestService {
    private Log log = LogFactory.getLog(this.getClass());
    @Autowired
    private CustomerRepository cust;
    @Autowired
    private OptionDetailRepository optionDetail;
    public List<Custom> findCustName() throws ParseException {
        List<Object[]> lists = cust.findCusName();
        Object[] oo = lists.get(0);
        System.out.println((String)oo[1]);
        return converts(lists);
    }
    public List<Custom> converts(List<Object[]> obj) throws ParseException {
        List<Custom> str = new ArrayList<Custom>();
        for(Object[] ss : obj){
            Custom cu = new Custom();
            Integer ig = Integer.valueOf(ss[0].toString());
            cu.setId(ig);
            cu.setName((String)ss[1]);
            String dd = ss[2].toString();
            cu.setDates(dd);
            str.add(cu);
        }
        return str;
    }

    //根据用户调查表查询关联属性

    public OptionConvert findAllFields(String cusId){
        List<Object[]> lists = cust.findAllCustomer(cusId);
        return opConvert(lists);
    }
    public OptionConvert findPollFields(String pollId){
        List<Object[]> lists = optionDetail.findFields(pollId);
        return opConvert(lists);
    }




    //转化数据类型
    public OptionConvert opConvert(List<Object[]> lists){
        OptionConvert oc = new OptionConvert();
        Object[] oo = lists.get(0);
        oc.setId(Integer.valueOf(oo[0].toString()));
        oc.setNames((String)oo[1]);
        oc.setCreateTime(String.valueOf(oo[2].toString()));
        //一级分类集合
        List<OptionFirst> fir = new ArrayList<OptionFirst>();
        //二级分类集合
        List<OptionSecond> sec = new ArrayList<OptionSecond>();
        boolean bn = true;
        for(Object[] obj : lists){
            if(obj[5]!=null) {
                List<OptionThred> ors = new ArrayList<OptionThred>();
                if (obj[7] != null) ors.add(new OptionThred((String)obj[7]));
                if (obj[8] != null) ors.add(new OptionThred((String)obj[8]));
                if (obj[9] != null) ors.add(new OptionThred((String)obj[9]));
                if (obj[10] != null) ors.add(new OptionThred((String)obj[10]));
                if (obj[11] != null) ors.add(new OptionThred((String)obj[11]));
                if (obj[12] != null) ors.add(new OptionThred((String)obj[12]));
                if (obj[13] != null) ors.add(new OptionThred((String)obj[13]));
                if (obj[14] != null) ors.add(new OptionThred((String)obj[14]));
                Integer ids = Integer.valueOf(obj[3].toString());
                sec.add(new OptionSecond(ids, (String)obj[4], Integer.valueOf(obj[5].toString()), ors));
            }else{
                if(fir.size()>0) {
                    for (OptionFirst ft : fir) {
                        if (ft.getPagename().equals((String) obj[4])) bn = false;
                    }
                }
                if(bn==true) {
                    OptionFirst op = new OptionFirst();
                    op.setId(Integer.valueOf(obj[3].toString()));
                    op.setPagename((String) obj[4]);
                    fir.add(op);
                }
            }
        }
        List<OptionFirst> first = new ArrayList<OptionFirst>();
        for(OptionFirst of : fir){
            List<OptionSecond> second = new ArrayList<OptionSecond>();
            for(OptionSecond os : sec){
                int a = os.getParentId();
                int b = of.getId();
                if(a==b){
                    second.add(os);
                }
            }
            of.setSelect(second);
            first.add(of);
        }
        oc.setArr(first);
        return oc;
    }



    //存储用户与调查登记表的信息
    public void saveUserOption(){

    }
}
